<script setup lang="ts">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { delObjs, fetchList, getFieldList, startFlow } from '/@/api/common';
import { useRouter } from 'vue-router';
import { getEntityByLabel } from '/@/api/admin/metaEntity';
import { useMessage, useMessageBox } from '/@/hooks/message';

const rule = ref({});

const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const FlowDetails = defineAsyncComponent(() => import('./flowDetails.vue'));

// 定义变量内容
const formDialogRef = ref();
const flowDetailsDialogRef = ref();
const router = useRouter();
const tableLabel = router.currentRoute.value.query.label;
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const showFields = ref();
const searchFields = ref();
const entity = ref();
getFieldList(tableLabel).then((res) => {
	showFields.value = res.data.showFields;
	searchFields.value = res.data.searchFields;
});

getEntityByLabel(tableLabel).then((res) => {
	res.data.formJson.formJson.forEach((item) => {
		if (!(item.props instanceof Object)){
			item.props = {}
		}
	});
	rule.value = res.data.formJson.formJson;
	entity.value = res.data;
});

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		label: tableLabel,
		data: {},
	},
	pageList: fetchList,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	// queryRef.value?.resetFields();
	state.queryForm.data = {};
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(tableLabel, ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

const openFlow = (id) => {
	console.log(id);
};

const startFlowByLabelAndId = (label: string, id: string) => {
	state.loading = true;
	startFlow(label, id)
		.then((res) => {
			getDataList(false);
		})
		.catch((err) => {
			useMessage().error(err.msg);
		});
};
</script>

<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item v-for="item in searchFields" :prop="item.label">
						<el-input :placeholder="'请输入' + item.name" v-model="state.queryForm.data[item.physicalName]" />
					</el-form-item>

					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList"> 查 询</el-button>
						<el-button icon="Refresh" @click="resetQuery">重 置</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog(tableLabel, {})"> 新 增 </el-button>
					<el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'admin_metaField_del'" @click="handleDelete(selectObjs)">
						删 除
					</el-button>
				</div>
			</el-row>

			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="#" width="100" />
				<el-table-column v-for="item in showFields" :prop="item.physicalName" :label="item.name" show-overflow-tooltip />
				<el-table-column label="操作" width="650">
					<template #default="scope">
						<el-button
							icon="edit-pen"
							v-if="entity.flowStatus == '1' && !scope.row.process_instance_id"
							text
							type="primary"
							@click="startFlowByLabelAndId(tableLabel, scope.row.id)"
							>发起流程
						</el-button>
						<el-button icon="edit-pen" text type="primary" @click="formDialogRef.openDialog(tableLabel, scope.row)"> 编辑 </el-button>
						<el-button icon="delete" text type="primary" @click="handleDelete([scope.row.id])">删除</el-button>
						<el-button
							v-if="scope.row.process_instance_id"
							icon="Operation"
							text
							type="primary"
							@click="flowDetailsDialogRef.openDialog(scope.row.process_instance_id,entity.formJson,scope.row,'')"
							>流程详情
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<form-dialog ref="formDialogRef" :rule="rule" @refresh="getDataList(false)" />
		<flow-details ref="flowDetailsDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<style scoped lang="scss"></style>
